package com.yycx.module.bus.provider.controller.hy;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.vo.OrderCustomReturn;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yycx.common.mybatis.model.*;

import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RestController;
import com.yycx.common.springmvc.base.BaseController;
import com.yycx.module.bus.client.entity.hy.BusHyorderReturn;
import com.yycx.module.bus.provider.service.hy.BusHyorderReturnService;


/**
 * 回款明细控制器
 *
 * @author flyme
 * @date 2023-11-23
 */
@RestController
@RequestMapping("/bus/bhr/")
public class BusHyorderReturnController extends BaseController<BusHyorderReturnService, BusHyorderReturn> {

    @Autowired
    private SysCompanyService companyService;

    @ApiOperation(value = "回款明细-分页列表", notes = "回款明细分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        return bizService.pageList(params);
    }

    @ApiOperation(value = "回款明细-列表", notes = "回款明细列表")
    @GetMapping(value = "list")
    public ResultBody list(@RequestParam(required = false) Map params) {
        return bizService.listEntityMap(params);
    }

    @ApiOperation(value = "回款明细-添加", notes = "添加回款明细")
    @PostMapping(value = "save")
    public ResultBody save(@RequestParam(required = false) Map params) {
        return bizService.add(params);
    }

    @ApiOperation(value = "回款明细-更新", notes = "更新回款明细")
    @PostMapping(value = "update")
    public ResultBody update(@RequestParam(required = false) Map params) {
        return bizService.edit(params);
    }

    @ApiOperation(value = "回款明细-删除", notes = "删除回款明细")
    @PostMapping(value = "remove")
    public ResultBody remove(@RequestParam(required = false) Map params) {
        return bizService.delete(params);
    }


    @ApiOperation(value = "回款明细-详情", notes = "回款明细详情")
    @GetMapping(value = "get")
    public ResultBody get(@RequestParam(required = false) Map params) {
        return bizService.get(params);
    }

    @ApiOperation(value = "拆分订单", notes = "拆分订单")
    @PostMapping(value = "chaiFen")
    public ResultBody chaiFen(OrderCustomReturn orderCustomReturn) {
        return bizService.chaiFen(orderCustomReturn);
    }


    @ApiOperation(value = "撤销拆分", notes = "撤销拆分")
    @PostMapping(value = "cancelChaiFen")
    public ResultBody cancelChaiFen(String orderDetailsReturnId) {
        return bizService.cancelChaiFen(orderDetailsReturnId);
    }

    @ApiOperation(value = "修改回款状态", notes = "修改回款状态")
    @PostMapping(value = "updateReturnState")
    public ResultBody updateReturnState(Long orderDetailsReturnId) {
        bizService.updateReturnState(orderDetailsReturnId);
        return ResultBody.msg("修改成功");
    }

    @ApiOperation(value = "票折", notes = "票折")
    @PostMapping(value = "discount")
    public ResultBody discount(@RequestParam String orderDetailsReturnId, @RequestParam BigDecimal saleAmount) {
        return bizService.addDiscountDetails(orderDetailsReturnId, saleAmount);
    }

    @ApiOperation(value = "更新超期金额", notes = "更新超期金额")
    @PostMapping(value = "updateOverAmount")
    public ResultBody updateOverAmount(@RequestParam String orderDetailsReturnId, @RequestParam Integer settleOverDay, @RequestParam BigDecimal settleOverPrice, @RequestParam BigDecimal overDateAmount) {
        boolean tag = bizService.updateOverAmount(orderDetailsReturnId, settleOverDay, settleOverPrice, overDateAmount);
        return ResultBody.msg("更新成功");
    }


    @ApiOperation(value = "同步回款数据", notes = "同步回款数据")
    @PostMapping(value = "syncToReturn")
    public ResultBody syncToReturn(@RequestParam String orderDetailsId) {
        return bizService.syncToReturn(orderDetailsId);
    }

    /**
     * 导出数据
     *
     * @param params
     * @param request
     * @param response
     */
    @PostMapping(value = "export")
    public void export(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        long date = DateUtils.getCurrentTimestamp();
        String handlerName = MapUtil.getStr(params, "handlerName");
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        String fileName = company.getCompanyName() + "-" + MapUtil.getStr(params, "fileName");
        bizService.export(params, request, response, fileName + DateUtil.formatDate(DateUtil.date()), fileName, handlerName);
    }

    /**
     * 导出数据
     *
     * @param params
     * @param request
     * @param response
     */
    @PostMapping(value = "exportTemplate")
    public void exportTemplate(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        String handlerName = MapUtil.getStr(params, "handlerName");
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        String fileName = company.getCompanyName() + "-" + MapUtil.getStr(params, "fileName");
        bizService.templateExport(params, request, response, fileName + DateUtil.formatDate(DateUtil.date()), fileName, handlerName);
    }

}
